package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.pojo.Community;
import com.qf.pojo.JSONResult;
import com.qf.pojo.Tenement;
import com.qf.pojo.Worker;
import com.qf.queryVo.WorkerVo;
import com.qf.service.WorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("worker")
public class WorkerController {
    @Autowired
    private WorkerService workerService;

    @RequestMapping("test")
    public Object test() {
        WorkerVo workerVo = new WorkerVo();
        workerVo.setTenementId(1);
        List<Worker> workers = workerService.findAll(workerVo);
        return workers;
    }


    @RequestMapping("findAll")
    public JSONResult findAll(
            @RequestParam(value = "page",required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "limit",required = false,defaultValue = "5") Integer pageSize,
            WorkerVo workerVo) {
        System.out.println("===========================");
        System.out.println(workerVo);
        JSONResult jsonResult = new JSONResult();
        PageHelper.startPage(pageNum,pageSize);
        List<Worker> workers = workerService.findAll(workerVo);
//        List<Worker> workers = workerService.findAll();
        PageInfo<Worker> pageInfo = new PageInfo<>(workers);
        jsonResult.setCode(0);
        jsonResult.setCount(pageInfo.getTotal());
        jsonResult.setMsg("success");
        jsonResult.setData(workers);
        return jsonResult;
    }

    @RequestMapping("removeOne")
    public String removeOne(Integer id) {
        workerService.removeOne(id);
        return "success";
    }

    @RequestMapping("removeAll")
    public String removeAll (Integer[] ids) {
        workerService.removeList(Arrays.asList(ids));
        return "success";
    }

    @RequestMapping("updateWorker")
    public String updateWorker(Worker worker) {
        workerService.updateWorker(worker);
        return "success";
    }

    @RequestMapping("addWorker")
    public JSONResult addWorker(Worker worker) {
        workerService.addWorker(worker);
        JSONResult jsonResult = new JSONResult();
        jsonResult.setCode(10000);
        return jsonResult;
    }

    @RequestMapping("findById")
    public Worker findById(Integer id) {
        Worker worker = workerService.findById(id);
        return worker;
    }

    //回返数据获得小区列表
    @RequestMapping("toGetCommunity")
    public JSONResult toGetCommunity() {
        List<Community> communityList = workerService.toGetCommunity();
        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(communityList);
        return jsonResult;
    }


    //回返数据获得小区列表
    @RequestMapping("toGetTenement")
    public JSONResult toGetTenement() {
        List<Tenement> tenementList = workerService.toGetTenement();
        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(tenementList);
        return jsonResult;
    }

}
